#include "Screen.h"

#include <stdio.h>

//------------------------------------------------------------------------------

Screen::Screen() {
	clear();
}


//------------------------------------------------------------------------------

Screen::~Screen() {
}


//------------------------------------------------------------------------------

void Screen::set( int x, int y, char c ) {
	screen[x][y] = c;
}

//------------------------------------------------------------------------------

void Screen::drawSnake( const Snake& snake ) {
	Snake::Direction* tail = snake.getTail();

	int x = snake.getX();
	int y = snake.getY();
	screen[x][y] = '@';
	for( int i = 0; i < snake.getLength(); i++ ) {


		if( tail[i] == Snake::U ) {
			y--;
		} else if( tail[i] == Snake::D ) {
			y++;
		} else if( tail[i] == Snake::L ) {
			x--;
		} else if( tail[i] == Snake::R ) {
			x++;
		}

		if( x >= WIDTH ) {
			x = 0;
		} else if( x < 0 ) {
			x = WIDTH-1;
		}

		if( y >= HEIGHT ) {
			y = 0;
		} else if( y < 0 ) {
			y = HEIGHT-1;
		}
		screen[x][y] = 'X';
	}
}

//------------------------------------------------------------------------------

void Screen::drawApple( const Apple& apple ) {
	screen[apple.getX()][apple.getY()] = '$';
}

//------------------------------------------------------------------------------

bool Screen::check( int x, int y ) {
	if( screen[x][y] == 'X' ) {
		return true;
	}

	return false;
}

//------------------------------------------------------------------------------

void Screen::paint() {
	for( int i = 0; i < HEIGHT; i++ ) {
		for( int j = 0; j < WIDTH; j++ ) {
			putchar( screen[j][i] );
		}
		putchar( '\n' );
	}
}

//------------------------------------------------------------------------------

void Screen::clear() {
	for( int i = 0; i < HEIGHT; i++ ) {
			for( int j = 0; j < WIDTH; j++ ) {
				screen[j][i] = ' ';
			}
		}
}

//------------------------------------------------------------------------------
